<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100px;
        height: 100px;
        background: red;
        position: absolute;
        left: 0px;
        top: 0px;
      }
    </style>
  </head>

  <body>
    <div class="box"></div>
    <script>
      // document.addEventListener("DOMContentLoaded")
      // 定义一个运动函数
      function move(ele, dir, target) {
        return new Promise((resolve) => {
          ele.style.transition = "all 1s"; // 设置元素的过渡；
          ele.style[dir] = target;
          ele.addEventListener("transitionend", resolve);
        });
      }
      let boxEle = document.querySelector(".box");
      boxEle.onclick = async function () {
        // 点击之后触发 方块的运动
       try{ await move(boxEle, "left", "300px");
        await move(boxEle, "top", "300px");
        await move(boxEle, "left", "0px");
        await move(boxEle, "top", "0px");}
      catch(err){
        console.log(err)
      }}

    
    </script>
  </body>
</html>
